<template>
  <div class="ma-content-block lg:flex justify-between p-4">
    <!-- CRUD 组件 -->
    <ma-crud :options="options" :columns="columns" ref="crudRef">
      <template #name="{ record }">
        <a-link @click="openInfo(record)">{{ record.name }}</a-link>
      </template>
      <template #tableAfterButtons>
        <a-button v-auth="['logistics:price:priceExecl']" type="primary" @click="ExeclFile()">
          <icon-upload/>
          导入
        </a-button>
      </template>
    </ma-crud>
    <price-info ref="priceInfo"/>
    <add-modal ref="addModalRef"></add-modal>
  </div>
</template>
<script setup>
import {ref, reactive, provide} from 'vue'
import price from '@/api/logistics/price'
import PriceInfo from './components/info.vue'
import {getDictData} from '@/utils/crud/dict'
import dicthelper from "@/utils/dicthelp.js";
import AddModal from "@cps/actionsNew/addModal.vue";
import { auth, role } from '@/utils/common'
const addModalRef = ref()
const crudRef = ref()
const priceInfo = ref()
const changeType = ref(1)
const openInfo = (record) => {
  priceInfo.value.open(record)
}
const ExeclFile = () =>{
  addModalRef.value.open([
    {
      title: "类型", dataIndex: "type", formType: 'radio',type:'button', search: true,hide: true,
      dict: {name: "PriceType", props: {label: "title", value: "key"}},
      commonRules: {required: true, message: "请输入类型"},
    },
    {
      title: "币种",
      dataIndex: "currency",
      formType: "select",
      addDefaultValue:'CNY',
      dict: {
        name: "currencyType",
        props: {
          label: "title",
          value: "key"
        },
        translation: true
      },
      commonRules: {
        required: true,
        message: "请输入币种"
      },
    },
    {
      title: "状态",
      dataIndex: "status",
      formType: "select",
      addDefaultValue: 1,
      dict: {
        data: [
          {value: 1, label: '正常'},
          {value: 0, label: '关闭'},
        ],
        translation: true,
      },
      commonRules: {
        required: true,
        message: "请输入状态"
      }
    },
    {
      title: "用户",
      dataIndex: "user",
      formType: "select",
      dict: dicthelper.customer,
      multiple: true,
      virtualListProps: {height: 200,}
    },
    {title: '拣货起始时间',
    dataIndex: "start_time",
    formType: "date",
    showTime: true,
    commonRules: {required: true, message: "请选择拣货起始时间"},
    // hide:true,
    
    },
    {title: '拣货截止时间',
      dataIndex: "end_time",
      formType: "date",
      showTime: true,
      commonRules: {required: true, message: "请选择拣货截止时间"},
    },
    {title: '下单起始时间',
      dataIndex: "create_start_time",
      formType: "date",
      showTime: true,
      customRender: ({record}) => {
        return record.create_start_time ? record.create_start_time : '-'
      },
      // hide:true,
    },
    {title: '下单截至时间',
      dataIndex: "create_end_time",
      formType: "date",
      showTime: true,
      customRender: ({record}) => {
        return record.create_end_time ? record.create_end_time : '-'
      },
    },
    {
      formType: 'grid-tailwind',
      colNumber: 6,
      cols: [
        {
          formList: [
            {
                title: "文件",
                dataIndex: "file",
                type: 'file',
                formType: "upload",
                required: true,
                rules:{required: true,message:'请上传价格文件'}
            },
          ]
        },
        // {
        //   formList: [
        //     {
        //       title: '下载模板',
        //       formType: 'button',
        //       onClick: (from) => {
        //         downloadImportTemplate('priceExecl')
        //       }
        //       },
        //   ]
        // },
      ],
    },
  ],{
    viewType: 'modal',
  },price.priceExecl,'导入费用',crudRef.value.refresh()
    
  )
}
const options = reactive({
  id: 'price',
  pageLayout: 'fixed',
  rowSelection: {
    showCheckedAll: true
  },
  pk: 'id',
  operationColumn: true,
  operationColumnWidth: 160,
  formOption: {
    viewType: 'drawer',
    width: "70%"
  },
  tabs: {
    dataIndex: 'type',
    onChange: function (value) {
      changeType.value = value
      let columnService = crudRef.value.getColumnService()
      columnService.get('supplier_service_id').setAttr('hide', value != 2)
      columnService.get('service_info.name').setAttr('hide', value == 2)
      columnService.get('user').setAttr('hide', value == 2)
      columnService.get('customer_grade').setAttr('hide', value == 2)
      columnService.get('seller').setAttr('hide', value == 2)
    },
  },
  api: price.getList,
  recycleApi: price.getRecycleList,
  add: {
    show: true,
    api: price.save,
    auth: ['logistics:price:save']
  },

  afterEdit: (params) => {
    priceInfo.value.refreshRecode()
    return params
  },
  edit: {
    show: true,
    api: price.update,
    auth: ['logistics:price:update']
  },
  delete: {
    show: true,
    api: price.deletes,
    auth: ['logistics:price:delete'],
    realApi: price.realDeletes,
    realAuth: ['logistics:price:realDeletes']
  },
  recovery: {
    show: true,
    api: price.recoverys,
    auth: ['logistics:price:recovery']
  }
})

const columns = reactive([
  {
    title: "主键",
    dataIndex: "id",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    commonRules: {
      required: true,
      message: "请输入主键"
    }
  },
  {
    title: "类型", dataIndex: "type", formType: 'radio',type:'button', search: true,hide: true,
    dict: {
      url: 'logistics/price/typeList',
      // name: "PriceType", props: {label: "title", value: "key"}
    },
    commonRules: {required: true, message: "请输入类型"},
    editDisplay: false,
    onControl:(val)=>{                                                                                                                                                                                              
      return {
        supplier_service_id:{display: val == 2},
        estimation:{display: val == 2},
        service:{display: val != 2},
        user:{display: val != 2},
        customer_grade:{display: val != 2},
        seller:{display: val != 2},
      }
    }
  },
  {
    title: "预估",
    dataIndex: "estimation",
    formType: "radio",
    hide: true,
    addDefaultValue:0,
    dict:{
      data:[
        {value: 1, label: "是"},
        {value: 0, label: "否"}
      ]
    },
    commonRules: {required: true,} 
  },
  {
    title: "名称",
    dataIndex: "name",
    formType: "input",
    search: true,
    commonRules: {
      required: true,
      message: "请输入名称"
    }
  },
  {
    title: "服务",
    dataIndex: "service_info.name",
    formType: "input",
    virtualListProps: {height: 200,},
    addDisplay: false,
    editDisplay: false,
  },
  {
    title: "服务",
    dataIndex: "service",
    formType: "select",
    dict: {
      url: 'logistics/service/list',
      props: {label: 'name', value: 'id'},
      params: {
        select: 'id,name'
      },
      translation: true,
    },
    commonRules: {
      required: true,
      message: "请选择服务"
    },
    search: true,
    hide: true
  },
  {
    title: "收货区域",
    dataIndex: "area",
    formType: "select",
    dict: {
      url: 'warehouse/area/list',
      props: {label: 'name', value: 'id'},
      params: {
        select: 'id,name'
      },
      translation: true,
    },
    multiple: true,
    // hide: true
  },
  {
    title: "优先级",
    dataIndex: "level",
    formType: "input",
    search: true
  },
  {
    title: "用户",
    dataIndex: "user",
    formType: "select",
    dict: dicthelper.customer,
    multiple: true,
    search: true,
    virtualListProps: {height: 200,}
  },
  {
    title: "用户等级",
    dataIndex: "customer_grade",
    formType: "select",
    dict: {
      url: 'customers/customerGrade/list',
      props: {label: 'name', value: 'id'},
      params: {
        select: 'id,name'
      },
      translation: true,
    },
    multiple: true,
  },
  {
    title: "所属销售",
    dataIndex: "seller",
    formType: "select",
    dict: {
      url: 'system/user/list',
      props: {label: 'nickname', value: 'id'},
      params: {
        select: 'id,nickname'
      },
      translation: true,
    },
    multiple: true,
    virtualListProps: {height: 200,},
    hide: true
  },
  {
    title: "最小重量",
    dataIndex: "min_weight",
    formType: "input",
    // search: true
  },
  {
    title: "最大重量",
    dataIndex: "max_weight",
    formType: "input",
    // search: true
  },
  {
    title: "燃油率",
    dataIndex: "oil_rate",
    formType: "input",
    hide: true
  },
  {
    title: "币种",
    dataIndex: "currency",
    formType: "select",
    dict: {
      name: "currencyType",
      props: {
        label: "title",
        value: "key"
      },
      translation: true
    },
    search: true,
    commonRules: {
      required: true,
      message: "请输入币种"
    },
    hide: true
  },
  {
    title: "计价单位",
    dataIndex: "charge_mode",
    formType: "select",
    dict: {
      name: "ChargeType",
      props: {
        label: "title",
        value: "key"
      },
      translation: true
    },
    // search: true,
    commonRules: {
      required: true,
      message: "请输入计价单位"
    },
    hide: true
  },
  {
    title: "状态",
    dataIndex: "status",
    formType: "select",
    addDefaultValue: 1,
    dict: {
      data: [
        {value: 1, label: '正常'},
        {value: 0, label: '关闭'},
      ],
      translation: true,
    },
    search: true,
    commonRules: {
      required: true,
      message: "请输入状态"
    }
  },
  {
    title: "供应商服务",
    dataIndex: "supplier_service_id",
    virtualListProps:{},
    dict: {
      url: 'logistics/supplier/list',
      props: {
        label: 'name',
        value: 'id'
      },
      translation: true
    },
    commonRules: {
      required: true,
      message: "请选择服务"
    },
    formType: "select",
    search: true
  },
  {
    title: "最后结束时间",
    dataIndex: "1",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    // hide: true,
    showTime: true
  },
  {
    title: "创建者",
    dataIndex: "created_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "更新者",
    dataIndex: "updated_by",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  },
  {
    title: "创建时间",
    dataIndex: "created_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "更新时间",
    dataIndex: "updated_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "删除时间",
    dataIndex: "deleted_at",
    formType: "date",
    addDisplay: false,
    editDisplay: false,
    hide: true,
    showTime: true
  },
  {
    title: "备注",
    dataIndex: "remark",
    formType: "input",
    addDisplay: false,
    editDisplay: false,
    hide: true
  }
])
provide('getDictData', getDictData)
provide('crudRef', crudRef)
</script>
<script> export default {name: 'logistics:price'} </script>